import { Resolve, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Injectable } from '@angular/core';
import { Lyric, Song } from '../entity';
import { SongService } from 'src/app/shared';
import { forkJoin, Observable } from 'rxjs';
import { first } from 'rxjs/operators';

type SongDetailModel = [Song, Lyric];

@Injectable()
export class SongDetailResolve implements Resolve<SongDetailModel> {

  constructor(
    private songService: SongService
  ) { }

  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<SongDetailModel> {
    const id = route.paramMap.get('id');
    return forkJoin([
      this.songService.getSongDetail(id),
      this.songService.getLyric(Number(id))
    ]).pipe(first());
  }

}
